﻿package src.flip{
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.media.*;
	import flash.net.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	import src.events.*;
	import src.utils.*;
	import src.caurina.transitions.*;
	public final class Pages extends MovieClip {

		private var flip:Flip = Flip.flip;
		private var config:Object = Flip.config;

		private var flipping:Boolean = false;
		private var current:Number = 1;
		private var index:Number = 1;

		private var page_list:Array;
		private var total:Number = 1;

		private var pages:Dictionary = new Dictionary();

		//mvc
		public var book:Sprite = new Sprite();
		public var pagesL:Sprite = new Sprite();
		public var pagesR:Sprite = new Sprite();
		//放大阅读
		private var cursor:IconZoomIn;

		public function Pages():void {
			book.addChild(pagesL);
			pagesR.x = config['page_width'];
			book.addChild(pagesR);
			addChild(book);
			//
			cursor = new IconZoomIn();
			cursor.mouseEnabled = false;
			cursor.mouseChildren = false;
			cursor.visible = false;
			addChild(cursor);
			//
			flip.addEventListener(FlipEvent.RESIZE, resizeHandler);
			flip.addEventListener(FlipEvent.FLIP_START, flipStart);
			flip.addEventListener(FlipEvent.FLIP_COMPLETED, flipCompleted);
			//
			flip.addEventListener(FlipEvent.PAGE_PREV, prevHandler);
			flip.addEventListener(FlipEvent.PAGE_NEXT, nextHandler);
			flip.addEventListener(FlipEvent.PAGE_FIRST, firstHandler);
			flip.addEventListener(FlipEvent.PAGE_LAST, lastHandler);
			//
			book.addEventListener(MouseEvent.MOUSE_WHEEL, wheelHandler);
			//
			resizeHandler();
			// pages config =============================================
			page_list = [];
			var urls:String = config['pages'];
			if (urls) {
				page_list = urls.split(Flip.COMMA);
				if (page_list.length) {
					mvc();
				}
			}
		}
		//mvc ============================================================
		private function mvc():void {
			//trace(page_list);
			total = 0;
			for (var i:int = 0; i < page_list.length; i = i + 2) {
				pages[i + 1] = new Page(page_list[i]);
				pages[i + 2] = new Page(page_list[i + 1]);
				total = total + 2;
			}
			//trace(total);
			//
			Flip.bar.bt_left.visible = false;
			pages[1].load();
			pagesR.addChild(pages[1]);
			Flip.sendEvent(FlipEvent.ZOOMER_LOAD, {showL:false, showR:true, srcL:"", srcR:page_list[0]});
			//
			book.filters = Flip.getShadow();
			book.addEventListener(MouseEvent.MOUSE_OVER, overHandler);
			book.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
			book.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
			book.addEventListener(MouseEvent.CLICK, clickHandler);
		}
		//==============================================================================
		private function overHandler(e:MouseEvent):void {
			Mouse.hide();
			moveHandler();
			cursor.visible = true;
		}
		private function outHandler(e:MouseEvent):void {
			Mouse.show();
			cursor.visible = false;
		}
		private function moveHandler(e:MouseEvent = null):void {
			cursor.x = mouseX - 5;
			cursor.y = mouseY - 5;
		}
		private function clickHandler(e:MouseEvent):void {
			Flip.sendEvent(FlipEvent.ZOOMER_SHOW, {offsetX:mouseX, offsetY:mouseY});
		}
		private function wheelHandler(e:MouseEvent):void {
			if(e.delta < 0) {
				if (!Flip.zoomer.visible) {
					clickHandler(e);
				}
			}
		}
		//==============================================================================
		private function prevHandler(e:FlipEvent = null):void {
			pageHandler(index - 2);
		}
		private function nextHandler(e:FlipEvent = null):void {
			pageHandler(index + 2);
		}
		private function firstHandler(e:FlipEvent = null):void {
			pageHandler(1);
		}
		private function lastHandler(e:FlipEvent = null):void {
			pageHandler(total + 1);
		}
		private function pageHandler(num:Number):void {
			if (num < 1) {
				num = 1;
			} else if (num > total + 1) {
				num = total + 1;
			}
			if (num != current && ! flipping) {
				index = num;
				Flip.sendEvent(FlipEvent.FLIP_START);
			}
		}
		//开始翻书
		private function flipStart(e:FlipEvent = null):void {
			flipping = true;
			Flip.sendEvent(FlipEvent.ZOOMER_HIDE);
			//================================
			if (index > current) {
				if (pagesR.numChildren) {
					closePage(pagesR.getChildAt(pagesR.numChildren - 1) as Page);
				}
				if (pages[index]) {
					pages[index].load();
					pagesR.addChildAt(pages[index], 0);
				}
				//
				if (pages[index - 1]) {
					pages[index - 1].load();
					pagesL.addChild(pages[index - 1]);
					openPage(pages[index - 1]);
				}
			} else {
				if (pagesL.numChildren) {
					closePage(pagesL.getChildAt(pagesL.numChildren - 1) as Page);
				}
				if (pages[index - 1]) {
					pages[index - 1].load();
					pagesL.addChildAt(pages[index - 1], 0);
				}
				//
				if (pages[index]) {
					pages[index].load();
					pagesR.addChild(pages[index]);
					openPage(pages[index]);
				}
			}
			if (pages[index + 1]) {
				pages[index + 1].load();
			}
			if (pages[index + 2]) {
				pages[index + 2].load();
			}
			if (pages[index - 2]) {
				pages[index - 2].load();
			}
		}

		private function openPage(clip:Page):void {
			if (clip) {
				clip.smoothing = false;
				//return;
				clip.width = 0;
				if (pagesL.contains(clip)) {
					clip.x = config['page_width'];
					Tweener.addTween(clip, {x:0, width:config['page_width'], delay:config['speed'], time:config['speed'], transition:"linear", onComplete:function(){clip.smoothing = true;openHandler();}});
				} else {
					Tweener.addTween(clip, {width:config['page_width'], delay:config['speed'], time:config['speed'], transition:"linear", onComplete:function(){clip.smoothing = true;openHandler();}});
				}
			}
		}
		private function openHandler():void {
			Flip.sendEvent(FlipEvent.FLIP_COMPLETED);
		}
		private function closePage(clip:Page):void {
			if (clip) {
				clip.smoothing = false;
				//return;
				clip.width = config['page_width'];
				if (pagesL.contains(clip)) {
					clip.x = 0;
					Tweener.addTween(clip, {x:config['page_width'], width:0, time:config['speed'], transition:"linear"});
				} else {
					Tweener.addTween(clip, {width:0, time:config['speed'], transition:"linear"});
				}
			}
		}

		private function flipCompleted(e:FlipEvent = null):void {
			current = index;
			flipping = false;
			cleanSide(pagesL);
			cleanSide(pagesR);
			//show button left and right
			var showL:Boolean = Boolean(pagesL.numChildren);
			var showR:Boolean = Boolean(pagesR.numChildren);
			Flip.bar.bt_left.visible = showL;
			Flip.bar.bt_right.visible = showR;
			//
			Flip.sendEvent(FlipEvent.ZOOMER_LOAD, {showL:showL, showR:showR, srcL:page_list[index - 2], srcR:page_list[index - 1]});
		}

		private function cleanSide(container:DisplayObjectContainer):void {
			//移除宽为0的
			var child:Page;
			for (var i:int = 0; i < container.numChildren; i ++) {
				child = container.getChildAt(i) as Page;
				if (child.width == 0) {
					child.remove();
					container.removeChild(child);
				}
			}
			//移除第一个
			while (container.numChildren > 1) {
				child = container.getChildAt(0) as Page;
				child.remove();
				container.removeChild(child);
			}
		}
		//==============================================================================

		private function resizeHandler(e:FlipEvent = null):void {
			this.x = config['width'] * 0.5 - config['page_width'];
			this.y = (config['height'] - config['page_height'] - Flip.bar.bar_back.height) * 0.5;
		}

	}
}